* Imports data from parliamentary elections 2019
import delimited "$raw/nrw_2019.csv", parselocale(en_AT) groupseparator(.) decimalseparator(,) clear

drop v20

rename gebietsname municipality_name

* Generate municipality_id
replace v1 = subinstr(v1,"G","",.)
destring v1, generate(municipality_id) force
drop v1

drop if municipality_id==. | municipality_id<10000 | municipality_id>=100000

* Change in municipal boundaries due to dissolution of the municipality of Murfeld
replace municipality_id = 61060 if municipality_id==61056 // St. Veit in der Südsteiermark
replace municipality_id = 61061 if municipality_id==61058 // Straß in der Steiermark
drop if municipality_id==62347 // Murfeld


rename wahlberechtigte eligible_voters
rename ungültige invalid_votes
rename gültige valid_votes
rename övp ovp
rename spö spo
rename fpö fpo
rename grüne grun


gen turnout = 100 * valid_votes/eligible_voters
gen sh_ovp = 100 * ovp / valid_votes
gen sh_spo = 100 * spo / valid_votes
gen sh_fpo = 100 * fpo / valid_votes
gen sh_neos = 100 * neos / valid_votes
gen sh_grun = 100 * grun / valid_votes

keep municipality_id municipality_name turnout sh_* ovp spo grun neos fpo

save "$edited/nrw_2019", replace
